<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '':'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem label="价格分类" type="remoteSelect"
                              v-model="baTable.form.items!.tid" prop="tid"
                              :input-attr="{ pk: 'ba_tariff.id', field: 'name', remoteUrl: '/admin/tariff/index' }"
                              placeholder="请选择分类"/>

                    <FormItem label="星级分类" type="remoteSelect"
                              v-model="baTable.form.items!.qid" prop="qid"
                              :input-attr="{ pk: 'ba_quality.id', field: 'name', remoteUrl: '/admin/quality/index' }"
                              placeholder="请选择分类"/>

                    <FormItem
                        type="remoteSelect"
                        prop="lid"
                        :label="t('auth.rule.Superior menu rule')"
                        v-model="baTable.form.items!.lid"
                        :placeholder="t('Click select')"
                        :input-attr="{
                            params: { isTree: true },
                            field: 'title',
                            remoteUrl: baTable.api.actionUrl.get('index'),
                        }"
                    />


                    <FormItem
                        type="remoteSelect"
                        prop="pid"
                        :label="t('auth.rule.Superior menu rule')"
                        v-model="baTable.form.items!.pid"
                        :placeholder="t('Click select')"
                        :input-attr="{
                            params: { isTree: true },
                            field: 'title',
                            remoteUrl: baTable.api.actionUrl.get('index'),
                        }"
                    />

                    <FormItem :label="t('tavern.image')" type="image" v-model="baTable.form.items!.image" prop="image" />
                    <FormItem :label="t('tavern.name')" type="string" v-model="baTable.form.items!.name" prop="name" :placeholder="t('Please input field', { field: t('tavern.name') })" />
<!--                    <FormItem :label="t('tavern.star')" type="string" v-model="baTable.form.items!.star" prop="star" :placeholder="t('Please input field', { field: t('tavern.star') })" />-->
                    <FormItem :label="t('tavern.blurb')" type="string" v-model="baTable.form.items!.blurb" prop="blurb" :placeholder="t('Please input field', { field: t('tavern.blurb') })" />
<!--                    <FormItem :label="t('tavern.province')" type="string" v-model="baTable.form.items!.province" prop="province" :placeholder="t('Please input field', { field: t('tavern.province') })" />-->
<!--                    <FormItem :label="t('tavern.city')" type="string" v-model="baTable.form.items!.city" prop="city" :placeholder="t('Please input field', { field: t('tavern.city') })" />-->
                    <FormItem label="所属城市" type="city" :input-attr="{level:2}" v-model="baTable.form.items!.city" prop="city"/>

                    <!--                    <FormItem label="所属省份" type="remoteSelect"-->
                    <!--                              v-model="baTable.form.items!.province_name" prop="province_name"-->
                    <!--                              :input-attr="{ pk: 'ba_area.name', field: 'name', remoteUrl: '/admin/area/index?limit=34',pagination:false }"-->
                    <!--                              placeholder="请选择所属省份"/>-->

                    <!--                    <FormItem :label="t('scenic.city_name')" type="string" v-model="baTable.form.items!.city_name"-->
                    <!--                              prop="city_name"-->
                    <!--                              :placeholder="t('Please input field', { field: t('scenic.city_name') })"/>-->


                    <el-form-item :label="t('tavern.address')">
                        <el-input
                            type="string"
                            v-model="baTable.form.items!.address"
                            prop="address"
                            :placeholder="t('tavern.address')"
                            readonly
                        >
                            <template #append>
                                <el-button @click="openMapPickPop">选择地点</el-button>
                            </template>
                        </el-input>
                    </el-form-item>

                    <el-row :gutter="10">
                        <el-col :span="12">
                            <FormItem
                                :label="t('tavern.lng')"
                                type="string"
                                v-model="baTable.form.items!.lng"
                                prop="lng"
                                :input-attr="{ readonly: true }"
                                :placeholder="t('Please input field', { field: t('tavern.lng') })"
                            />
                        </el-col>
                        <el-col :span="12">
                            <FormItem
                                :label="t('tavern.lat')"
                                type="string"
                                v-model="baTable.form.items!.lat"
                                prop="lat"
                                :input-attr="{ readonly: true }"
                                :placeholder="t('Please input field', { field: t('tavern.lat') })"
                            />
                        </el-col>
                    </el-row>
                    <FormItem :label="t('tavern.label')" type="string" v-model="baTable.form.items!.label" prop="label" :placeholder="t('Please input field', { field: t('tavern.label') })" />

<!--                    <FormItem :label="t('tavern.address')" type="string" v-model="baTable.form.items!.address" prop="address" :placeholder="t('Please input field', { field: t('tavern.address') })" />-->
<!--                    <FormItem :label="t('tavern.lat')" type="string" v-model="baTable.form.items!.lat" prop="lat" :placeholder="t('Please input field', { field: t('tavern.lat') })" />-->
<!--                    <FormItem :label="t('tavern.lng')" type="string" v-model="baTable.form.items!.lng" prop="lng" :placeholder="t('Please input field', { field: t('tavern.lng') })" />-->
<!--                    <FormItem :label="t('tavern.array')" type="array" v-model="baTable.form.items!.array" prop="array" :placeholder="t('Please input field', { field: t('tavern.array') })" />-->
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { inject, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'
import {getTencentmapKey} from "/@/api/backend/xmmap/tencentmap";
import {ElNotification} from "element-plus";
import {openTencentMapPickPop} from "/@/components/xmmap/tencentMap";

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const state = reactive({
    tencentMapKey: '',
})
// 获取腾讯地图key，可替换自己的接口
getTencentmapKey().then((res) => {
    state.tencentMapKey = res.data
})
const openMapPickPop = () => {
    if (state.tencentMapKey == '') {
        ElNotification({
            type: 'error',
            message: '请前往【系统配置】【第三方密钥】配置腾讯地图KEY',
        })
        return false
    }
    openTencentMapPickPop(state.tencentMapKey)
}
window.addEventListener('message', function (event) {
    if (event.data.type == 'tencent-map-data') {
        const currentData = JSON.parse(event.data.data)
        console.log('腾讯地图当前位置数据：', currentData)
        baTable.form.items!.poiname = currentData.poiname
        baTable.form.items!.address = currentData.poiaddress
        baTable.form.items!.lng = currentData.latlng.lng
        baTable.form.items!.lat = currentData.latlng.lat
    }
})

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    lid: [
        {
            validator: (rule: any, val: string, callback: Function) => {
                if (!val) {
                    return callback()
                }
                if (parseInt(val) == parseInt(baTable.form.items!.id)) {
                    return callback(new Error(t('auth.rule.The superior menu rule cannot be the rule itself')))
                }
                return callback()
            },
            trigger: 'blur',
        },
    ],
    pid: [
        {
            validator: (rule: any, val: string, callback: Function) => {
                if (!val) {
                    return callback()
                }
                if (parseInt(val) == parseInt(baTable.form.items!.id)) {
                    return callback(new Error(t('auth.rule.The superior menu rule cannot be the rule itself')))
                }
                return callback()
            },
            trigger: 'blur',
        },
    ],
    create_time: [buildValidatorData({ name: 'date', title: t('tavern.create_time') })],
    update_time: [buildValidatorData({ name: 'date', title: t('tavern.update_time') })],
})
</script>

<style scoped lang="scss"></style>
